'use strict';

module.exports = appInfo => {
  const config = (exports = {});

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1526898238052_8439';

  config.BASE_ARTICLE_URL = 'http://10.45.71.30:3000/article';

  // 添加 view 配置
  config.view = {
    defaultViewEngine: 'nunjucks',
    mapping: {
      '.tpl': 'nunjucks',
    },
  };

  // add your config here
  config.middleware = [ 'robot', 'errorHandler', 'policies' ];

  // robot
  config.robot = {
    ua: [ /curl/i, /Baiduspider/i ],
  };

  config.errorHandler = {
    match: '/api',
  };

  // egg-validate
  config.validate = {
    enable: true,
    package: 'egg-validate',
  };

  // mysql
  config.mysql = {
    // 单数据库信息配置
    client: {
      // host
      host: '192.168.188.16',
      // 端口号
      port: '3306',
      // 用户名
      user: 'root',
      // 密码
      password: '666666',
      // 数据库名
      database: 'egg_example_database',
    },
    // 是否加载到 app 上，默认开启
    app: true,
    // 是否加载到 agent 上，默认关闭
    agent: false,
  };

  // redis
  config.redis = {
    client: {
      host: '192.168.188.16',
      port: '6379',
      password: '666666',
      db: 0,
    },
  };

  // policies
  config.policies = {
    jwt: {
      secret: 'I5DDago4s1OW.',
      expiresIn: 60 * 60 * 24,
    },
    ignore: [ '/', '/api/login' ],
  };

  // security
  config.security = {
    csrf: {
      enable: false,
    },
    // domainWhiteList: [ 'http://localhost:3000/', 'http://10.45.71.30:3000' ],
  };

  config.cors = {
    origin(ctx) {
      const {
        headers: { origin },
      } = ctx.request;
      const origins = [
        'http://localhost:2048',
        'http://localhost:8000',
        'http://127.0.0.1:8000',
        'http://10.45.71.30:8000',
        'http://localhost:3000',
        'http://localhost:3001',
        'http://10.45.71.30:3000',
        'http://10.45.71.30:3001',
        'http://192.168.188.3:3000',
        'http://192.168.188.3:3001',
        'http://10.45.71.30:2048',
      ];
      if (origins.includes(origin)) {
        return origin;
      }
      return 'http://localhost:8000';
    },
    credentials: true,
    allowMethods: 'GET, HEAD, PUT, POST, DELETE, PATCH, OPTION',
  };

  // ali oss
  config.oss = {
    client: {
      accessKeyId: 'dSlPZZoS5UUr5Yhb',
      accessKeySecret: 'ba6loXDSiIDW4gexRi3YzaGFF9Fmi8',
      bucket: 'egg-example',
      endpoint: 'oss-cn-shanghai.aliyuncs.com',
      timeout: '60s',
    },
  };

  // sequelize
  config.sequelize = {
    dialect: 'mysql',
    database: 'egg_example_database',
    host: '192.168.188.16',
    port: '3306',
    username: 'root',
    password: '666666',
    operatorsAliases: false,
  };

  return config;
};
